package CreationalPattern;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class XMLUtil {
    // 从XML读取配置，不需要修改客户端代码去改变创造不同的类
    public static String getFruitType() {
        try {
            // 创建文档对象
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document;
            document = documentBuilder.parse("src\\main\\resources\\config.xml");

            // 获取文本节点
            NodeList nodeList = document.getElementsByTagName("chartType");
            Node classNode = nodeList.item(0).getFirstChild();
            String Fruittype = classNode.getNodeValue().trim();
            return Fruittype;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
